home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTE19 / TIMER.C < prev    next >
C/C++ Source or Header  |  1996-04-29  |  7KB  |  244 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "Timer.h"
  5. #include <vfw.h>
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18.  
  19.  
  20. HINSTANCE hInst;   // current instance
  21.  
  22. LPCTSTR lpszAppName = "MyApp";
  23. LPCTSTR lpszTitle   = "My Application"; 
  24.  
  25. // the rest of the stuff
  26. //......................
  27.  
  28. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  29.  
  30.  
  31. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  32.                       LPTSTR lpCmdLine, int nCmdShow)
  33. {
  34.    MSG      msg;
  35.    HWND     hWnd; 
  36.    WNDCLASS wc;
  37.  
  38.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  39.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  40.    wc.cbClsExtra    = 0;                      
  41.    wc.cbWndExtra    = 0;                      
  42.    wc.hInstance     = hInstance;              
  43.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  44.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  45.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  46.    wc.lpszMenuName  = lpszAppName;              
  47.    wc.lpszClassName = lpszAppName;              
  48.  
  49.    if ( IS_WIN95 )
  50.    {
  51.       if ( !RegisterWin95( &wc ) )
  52.          return( FALSE );
  53.    }
  54.    else if ( !RegisterClass( &wc ) )
  55.       return( FALSE );
  56.  
  57.    hInst = hInstance; 
  58.  
  59.    hWnd = CreateWindow( lpszAppName, 
  60.                         lpszTitle,    
  61.                         WS_OVERLAPPEDWINDOW, 
  62.                         CW_USEDEFAULT, 0, 
  63.                         CW_USEDEFAULT, 0,  
  64.                         NULL,              
  65.                         NULL,              
  66.                         hInstance,         
  67.                         NULL               
  68.                       );
  69.  
  70.    if ( !hWnd ) 
  71.       return( FALSE );
  72.  
  73.    ShowWindow( hWnd, nCmdShow ); 
  74.    UpdateWindow( hWnd );         
  75.  
  76.    while( GetMessage( &msg, NULL, 0, 0) )   
  77.    {
  78.       TranslateMessage( &msg ); 
  79.       DispatchMessage( &msg );  
  80.    }
  81.  
  82.    return( msg.wParam ); 
  83. }
  84.  
  85.  
  86. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  87. {
  88.     WNDCLASSEX wcex;
  89.  
  90.    wcex.style         = lpwc->style;
  91.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  92.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  93.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  94.    wcex.hInstance     = lpwc->hInstance;
  95.    wcex.hIcon         = lpwc->hIcon;
  96.    wcex.hCursor       = lpwc->hCursor;
  97.    wcex.hbrBackground = lpwc->hbrBackground;
  98.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  99.    wcex.lpszClassName = lpwc->lpszClassName;
  100.  
  101.    // Added elements for Windows 95.
  102.    //...............................
  103.    wcex.cbSize = sizeof(WNDCLASSEX);
  104.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  105.                             IMAGE_ICON, 16, 16,
  106.                             LR_DEFAULTCOLOR );
  107.             
  108.    return RegisterClassEx( &wcex );
  109. }
  110.  
  111. HWND hMCIWnd = NULL;   // MCIWnd window handle
  112.  
  113. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115.    switch( uMsg )
  116.    {
  117.       case WM_CREATE :
  118.               // create MCIWnd window
  119.               //.....................
  120.  
  121.               hMCIWnd = MCIWndCreate(hWnd, hInst, 
  122.                                      WS_VISIBLE | 
  123.                                      WS_CHILD | 
  124.                                      WS_OVERLAPPED |
  125.                                      WS_CAPTION |
  126.                                      WS_BORDER |
  127.                                      MCIWNDF_NOTIFYMODE |
  128.                                      MCIWNDF_SHOWALL, 
  129.                                      "Sample.avi"
  130.                                     );
  131.                           
  132.               if (!hMCIWnd)
  133.               {
  134.                   MessageBox(hWnd, "Error creating MCIWnd window...", 
  135.                              NULL, MB_OK);
  136.                   DestroyWindow( hWnd );
  137.               }
  138.               break;
  139.  
  140.       case WM_COMMAND :
  141.               switch( LOWORD( wParam ) )
  142.               {
  143.                  // MCIWndGetActiveTimer()
  144.                  // MCIWndGetInactiveTimer()
  145.                  //
  146.                  // NOTE: As of this writing, an error exists in the VFW.H 
  147.                  //       file distributed with the WIN32SDK. The macro 
  148.                  //       definition is incorrectly terminated by a 
  149.                  //       semi-colon. 
  150.                  //       The semi-colon must be removed if these macros are
  151.                  //       to be used in an expression, or the compiler will
  152.                  //       generate errors.
  153.  
  154.                  
  155.                  case IDM_DEC_SETACTIVETIMER :
  156.                         {
  157.                            UINT uMs; // in milliseconds
  158.  
  159.                            uMs = MCIWndGetActiveTimer(hMCIWnd);
  160.  
  161.                            if (uMs >= 200)
  162.                            {
  163.                                uMs -= 100;
  164.                                MCIWndSetActiveTimer(hMCIWnd, uMs);
  165.                            }
  166.                         }
  167.                         break;
  168.  
  169.                  case IDM_INC_SETINACTIVETIMER : 
  170.                         {
  171.                            UINT uMs; // in milliseconds
  172.  
  173.                            uMs = MCIWndGetInactiveTimer(hMCIWnd);
  174.  
  175.                            uMs += 100;
  176.  
  177.                            MCIWndSetInactiveTimer(hMCIWnd, uMs);
  178.                         }
  179.                         break;
  180.  
  181.                  case IDM_RESET_TIMERS :
  182.                         MCIWndSetTimers(hMCIWnd, 500, 2000); // reset to defaults
  183.                         break;
  184.                  
  185.                  case IDM_ABOUT :
  186.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  187.                         break;
  188.  
  189.                  case IDM_EXIT :
  190.                         DestroyWindow(hWnd);
  191.                         break;
  192.               }
  193.               break;
  194.  
  195.       case WM_DESTROY :
  196.               // destroy MCIWnd, if one exists
  197.               //..............................
  198.  
  199.               if (hMCIWnd)
  200.                   MCIWndDestroy(hMCIWnd);
  201.  
  202.               PostQuitMessage(0);
  203.               break;
  204.  
  205.       default :
  206.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  207.    }
  208.  
  209.    return( 0L );               
  210. }
  211.  
  212.  
  213. LRESULT CALLBACK About( HWND hDlg,           
  214.                         UINT message,        
  215.                         WPARAM wParam,       
  216.                         LPARAM lParam)
  217. {
  218.    switch (message) 
  219.    {
  220.        case WM_INITDIALOG: 
  221.                return (TRUE);
  222.  
  223.        case WM_COMMAND:                              
  224.                if (   LOWORD(wParam) == IDOK         
  225.                    || LOWORD(wParam) == IDCANCEL)    
  226.                {
  227.                        EndDialog(hDlg, TRUE);        
  228.                        return (TRUE);
  229.                }
  230.                break;
  231.    }
  232.  
  233.    return (FALSE); 
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.